/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUInemeanshop;

import components.NFrame;
import components.NPanel;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import nemeanshop.Cashier;
import nemeanshop.NemeanShop;
import nemeanshop.TimeOut;

/**
 *
 * @author havardaxelsen
 */
public class GUIContent extends NFrame{
    
    private NemeanShop      ns          = new NemeanShop();
    private CardLayout      cl          = new CardLayout();
    private NPanel          cards       = new NPanel(cl);
    private String          s           = "";
    private MusLytter       mlytter     = new MusLytter();
    private Knappl          klytter     = new Knappl();
    private TimeOut         time        = new TimeOut(this, 2);
    private GUIMenu         menu;
    private HomePanel       hp          = new HomePanel(this);
    private GUICashier      gc          = new GUICashier(ns);
    private BasketPanel     bp          = new BasketPanel(ns);
    private GUILogin        login       = new GUILogin();
    private GUISettings     guis        = new GUISettings(this);
    private GUIProductlist  gpl         = new GUIProductlist(ns);
    private GUIStatistics   stats       = new GUIStatistics(ns);
    private LogInBox        loginbox;
    
    final static String     BP          = "BASKETPANEL";
    final static String     GC          = "CASHIER";
    final static String     HP          = "HOME";
    final static String     LOG         = "LOGIN";
    final static String     SETTINGS    = "SETTINGS";
    final static String     GPL         = "PRODUKTLISTE";
    final static String     STATS       = "STATISTICS";

    public GUIContent(String name) {
        super(name);
        loginbox = new LogInBox(ns, this, false, klytter);
        loginbox.setDialogLocation(this);
        this.setUndecorated(true);
        this.setLayout(new BorderLayout());
        menu = new GUIMenu(this, ns);
        bp.addMouseListener(mlytter);
        bp.addMouseMotionListener(mlytter);
        this.addKeyListener(klytter);
        this.addWindowFocusListener(new WindowAdapter() {
            public void windowGainedFocus(WindowEvent e) {
                requestFocusInWindow();
            }
        });
        cards.addMouseListener(mlytter);
        cards.addMouseMotionListener(mlytter);
        cards.add(login,LOG);       // Adds Login screen
        cards.add(gc,GC);           // Adds Cashier list
        cards.add(bp,BP);           // Adds Basket
        cards.add(hp, HP);          // Adds HomePanel
        cards.add(guis, SETTINGS);  // Adds Settings
        cards.add(gpl, GPL);        // Adds Product list
        cards.add(stats, STATS);    // Adds Statistics
        cards.setFocusable(false);
        cards.setBorder(BorderFactory.createEmptyBorder(5,20,5,20));
        
        add(cards, BorderLayout.CENTER);
        cl.show(cards, LOG);
    }
    
    public void setName(String n){
        
        this.setTitle(n);
    }
    
    
    private class MusLytter implements MouseListener, MouseMotionListener{

        
        @Override
        public void mouseClicked(MouseEvent me) 
        {
            time.update();
        }

        @Override
        public void mousePressed(MouseEvent me) {
          time.update();
        }

        @Override
        public void mouseReleased(MouseEvent me) {
            time.update();
        }

        @Override
        public void mouseEntered(MouseEvent me) {
            time.update();
        }

        @Override
        public void mouseExited(MouseEvent me) {
            time.update();
        }
        
        public void mouseMoved(MouseEvent me){
            time.update();
        }

        @Override
        public void mouseDragged(MouseEvent e)
        {
        }
    
}
    
    private class Knappl implements KeyListener {

        @Override
        public void keyTyped(KeyEvent ke) {
        }

        @Override
        public void keyPressed(KeyEvent ke) {
             
            
            
            if (ke.getKeyChar() == KeyEvent.VK_ENTER)
            {
                
                // Bruker logger inn. Cashier settes.
                if ( ns.getCashier()==null && ns.AuthorizeCashier(s)) 
                {
                    
                    showCard(GUIContent.HP);
                    add(menu, BorderLayout.NORTH);
                    
                    
                    try {
                        
                        menu.setCashier(ns.getCashier(s).toString());
                        s = "";
                        new Thread(time, "TimeOut").start();
                       
                   
                    } catch (Exception ex) {
                        Logger.getLogger(GUIContent.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                
                
                // Bruker er innlogget. Om produkt scannes, sendes man til "handlekurven" og adder det innscannede produkt.
                else if(ns.getCashier()!= null && ns.isProduct(s)){  
                
                    //KODE FOR Å LEGGE TIL PRODUKT I KURV
                    if(ns.getOrder() == null){
                        ns.createOrder();
                        try {
                            ns.getOrder().addProduct(ns.getProduct(s));
                            bp.updateOrder();
                        } 
                        catch (Exception ex) {
                            Logger.getLogger(GUIContent.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    else{
                        try {
                            ns.getOrder().addProduct(ns.getProduct(s));
                            bp.updateOrder();
                        } 
                        catch (Exception ex) {
                            Logger.getLogger(GUIContent.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    s = "";
                    showCard(GUIContent.BP);
                }
                
                // Admin kjøper varer så legg de til på admin
                else if(ns.getCashier()!= null && ns.getOrder() != null && ns.getOrder().getBasket().size() >0 && ns.getCashier(s) != null){
                    
                    ns.registerAdminOrder(ns.getOrder(), ns.getCashier(s).getUserID());
                    bp.reset();
                    ns.getOrder().reset();
                    s= "";
                    
                    }
                
                
                //Bruker logger inn med brukernavn og passord
                
                else if(ns.logIn(loginbox.un(), loginbox.pw())){
                    
                    showCard(GUIContent.HP);
                    add(menu, BorderLayout.NORTH);
                    
                    try {
                        String barcode  = ns.loginBc(loginbox.un(), loginbox.pw());
                        menu.setCashier(ns.getCashier(barcode).toString());
                        loginbox.reset();
                        s = "";
                        new Thread(time, "TimeOut").start();
                    
                    } catch (Exception ex) {
                        
                        Logger.getLogger(GUIContent.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                else{
                    
                    s= "";
                }
            }
            
            
            else if(ke.getKeyChar() == KeyEvent.VK_ESCAPE && ns.getCashier()==null){
                
                if(loginbox.isVisible()){
                    
                    loginbox.reset();
                }
                else{
                    
                    loginbox.setVisible(true);
                }
            }
            else if(ke.getKeyChar() == KeyEvent.VK_ESCAPE && ns.getCashier()!=null){
                
                ns.removeCashier();
                logOut();
            }
            else
            {
                s += ke.getKeyChar();
            }
        }


        @Override
        public void keyReleased(KeyEvent ke) {
        }
        }

        public void showCard(final String cardName)
        {
            EventQueue.invokeLater(new Runnable() {
            @Override
                public void run() {
                    cl.show(cards, cardName);
                }
            });
        }
        
        public void logOut(){
            EventQueue.invokeLater(new Runnable() {
            @Override
                public void run() {
                    ns.removeCashier();
                    remove(menu);
                    showCard(LOG);      
                    setFocusable(true);
                    time.setState();
                    ns.deleteOrder();
                    bp.reset();
                }
            });
       
        }
        
        public void updateLogoutTime(int i){
            this.time.update(i);
        }
        
        public int getLogoutTime(){
            return time.getMinutes();
        }
        
        public void updateGUIProductList()
        {
            gpl.update();
        }
        
        public void checkProducts()
        {
            if (ns.checkProducts())
            {
                menu.setNotification(true);
            }
            else
            {
                menu.setNotification(false);
            }
            updateGUIProductList();
        }
        
       
    
}
    
    
    

